package com.qf.secondkill.controller;

import com.qf.secondkill.util.RedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SecondKillController {

    public static Map<String,Integer> itemStock = new HashMap<>();
    public static Map<String,Integer> itemOrder = new HashMap<>();

    static{
        itemStock.put("riceSon",10000);
        itemOrder.put("riceSon",0);
    }


    /*
    //Zookeeper分布式锁
    @Autowired
    private CuratorFramework curatorFramework;

    @GetMapping("/second-kill")
    public String secondKill(String name) throws Exception {
        //1. 接收到需要购买的商品名称
        if(StringUtils.isEmpty(name)) {
            return "请传递商品名称!!!";
        }
        InterProcessMutex lock = new InterProcessMutex(curatorFramework,"/lock");

        // lock.acquire();  // 一直排队....  公平锁.
        if(lock.acquire(10, TimeUnit.MILLISECONDS)) {
            //---------------------------------------------------------------
            //2. 获取到商品剩余的库存 (模拟数据库操作)
            Integer stock = itemStock.get(name);
            Thread.sleep(200);
            //3. 判断库存数是否足够
            if (stock <= 0) {
                return "商品被抢光了!!!";
            }
            //4. 库存数 - 1
            itemStock.put(name, --stock);
            //5. 创建订单  订单数 + 1
            Integer order = itemOrder.get(name);
            itemOrder.put(name, ++order);
            //---------------------------------------------------------------------
        }
        lock.release();     // 释放锁.....





        //6. 返回,购买商品成功
        return "购买商品成功!!!!";
    }*/


    // Redis分布式锁
    @Autowired
    private RedisLock lock;

    @GetMapping("/second-kill")
    public String secondKill(String name) throws Exception {
        //1. 接收到需要购买的商品名称
        if(StringUtils.isEmpty(name)) {
            return "请传递商品名称!!!";
        }


        // Redisson  公平锁
        // Redis - List - 实现栈,队列结构
        // 秒杀商品100件
        // 客户端发送请求到tomcat,tomcat执行opsForList.rpush(...);
        // range 0 ~ 99
        if(lock.lock(name,System.currentTimeMillis() + "",3000)) {
            //---------------------------------------------------------------
            //2. 获取到商品剩余的库存 (模拟数据库操作)
            Integer stock = itemStock.get(name);
            Thread.sleep(200);
            //3. 判断库存数是否足够
            if (stock <= 0) {
                return "商品被抢光了!!!";
            }
            //4. 库存数 - 1
            itemStock.put(name, --stock);
            //5. 创建订单  订单数 + 1
            Integer order = itemOrder.get(name);
            itemOrder.put(name, ++order);
            // 释放锁资源
            lock.unlock(name);
            //---------------------------------------------------------------------
        }else {
            return "获取锁资源失败!!!";
        }
        //6. 返回,购买商品成功
        return "购买商品成功!!!!";
    }


    @GetMapping("/info")
    public String info(String name){
        String result = "剩余商品库存数为:" + itemStock.get(name) + ",商品订单数为:" + itemOrder.get(name);
        return result;
    }

}
